<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="clickShowAdd"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                label="序号"
                width="100"
                type="index"
                :index="indexMethod"
              />
              <el-table-column
                label="角色名称"
                width="240"
                prop="name"
              />
              <el-table-column
                label="描述"
                prop="description"
              />
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    size="small"
                    type="success"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 页码效果 -->
            <div
              class="block"
              style="margin-top:10px;display:flex;justify-content:flex-end"
            >
              <!-- <span class="demonstration">完整功能</span> -->
              <el-pagination
                :current-page="page"
                :page-sizes="[2, 3, 4, 5]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form
              label-width="120px"
              style="margin-top:50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗结构 -->
      <el-dialog
        :title="showTitle"
        :visible="showDialog"
        :close-on-click-modal="false"
        @close="closeDialog"
      >
        <el-form
          ref="roleForm"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item
            label="角色名称"
            prop="name"
          >
            <el-input
              v-model="form.name"
              placeholder="请输入角色名称"
            />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input
              v-model="form.description"
              placeholder="请输入角色描述"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="closeDialog">取消</el-button>
          <el-button
            type="primary"
            @click="submitBtn"
          >确认</el-button>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import {
  addRoleApi,
  delRoleListApi,
  getCompanyInfoApi,
  getRoleDetailApi,
  getRoleListApi,
  updateRoleApi
} from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      page: 1,
      pagesize: 2,
      roleList: [],
      total: 0,
      loading: false,
      showDialog: false,
      form: { name: '', description: '' },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', tirgger: 'blur' }]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    // 添加修改标题
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取部门列表信息
    async getRoleList() {
      this.loading = true
      const { data } = await getRoleListApi(this.page, this.pagesize)
      this.roleList = data.rows
      this.total = data.total
      this.loading = false
    },
    // 点击页码
    handleCurrentChange(val) {
      // console.log(val)
      this.page = val
      this.getRoleList()
    },
    /**
     * 修改页容量
     */
    handleSizeChange(val) {
      // console.log(val)
      this.pagesize = val
      this.getRoleList()
    },
    // 分页序号效果
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 删除
    delRole(id) {
      this.$confirm('确认要删除该角色吗?', '温馨提示')
        .then(async() => {
          await delRoleListApi(id) // 调用删除接口
          this.$message.success('删除角色成功')
          // 只要不是第一页 并且只有一条数据，就让page--
          if (this.roleList.length === 1 && this.page > 1) {
            this.page--
          }
          this.getRoleList() // 重新加载数据
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 打开弹层
    clickShowAdd() {
      this.showDialog = true
    },
    // 确认添加
    submitBtn() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        // 如果验证成功
        if (this.form.id) {
          await updateRoleApi(this.form)
          this.$message.success('修改成功')
        } else {
          await addRoleApi(this.form)
          this.$message.success('添加成功')
        }
        this.getRoleList()
        this.closeDialog()
      })
    },
    // 关闭弹层
    closeDialog() {
      this.showDialog = false
      // 关闭后清空表单数据
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 编辑角色
    async editRole(id) {
      this.showDialog = true
      const { data } = await getRoleDetailApi(id)
      this.form = data
    },
    // 展示公司信息
    async getCompanyInfo() {
      const { data } = await getCompanyInfoApi(this.getCompanyId)
      this.companyForm = data
    }
  }
}
</script>

<style>
</style>
